Game Development Using Python by James R. Parker

Game Development Using Python by James R. Parker

Author:James R. Parker
Language: eng
Format: epub, mobi
Publisher: Mercury Learning And Information
Published: 2018-12-06T05:00:00+00:00


FINITE STATE MACHINES

The idea that an NPC can be cruising, chasing, or avoiding is not especially profound, and clearly different behavior can be assigned to each mode or state. It is also convenient from the perspective of design to be able to break up the different behaviors into distinct parts, which can then be implemented independently. The use of the traditional computer science tool, the finite state machine, is a pretty natural way to deal with this kind of situation. Finite state machines, also called FSAs, are used in programming languages, computability, control systems, and artificial intelligence, and because they have been widely used, their properties are well known and efficient implementations abound.

We have seen the basic idea of an FSA when implementing the game states in Hockey Pong, for example. The basic idea of an FSA is a collection of states and of transitions between these states upon some input or calculation. The states have numbers, used in the implementation, and names, used by the designers and programmers as meanings of the states. In the situation described in the previous section, the AI vehicle starts out in the cruising state. If it encounters another AI car on the road ahead, it enters the overtake_AI state, and if it encounters the player’s car, it enters the overtake_player state. The behavior of the AI is quite different in each state, and its goals and methods of achieving them are distinct. Figure 5.4 shows a diagrammatic representation of an FSA, specifically one for the previous three states. It is essential to have a clear mechanism for moving between states and a clear plan for what to do while in each state.

Mathematically, an FSA is a simulated machine or mathematical construction consisting of a set of states, which are usually integers, a special state called the start state, a collection of input symbols or events, and a transition function that takes an input symbol and the current state and decides what the next state will be. The FSA begins a computation in the start state and enters states based on input symbols/events and the transition function. There can be a special state called the accept state that can be used to decide when the calculation is complete.

So, if we are in the cruising state (state 0) and an AI vehicle appears in front of us, we enter the overtake_AI state (1); if we are in the cruising state and the player’s car appears in front of us we enter the overtake_player state (2). These are the only state transitions out of state 0 in Figure 5.4.

While in the overtake_AI state, there are a couple of events that could take place. We could pass the AI car, or we could be blocked further. If we pass the car, we can go back to the cruising state again. If we are blocked—well, perhaps we need another state called delay in which we slow down and look for a change in the situation. The delay state will be state 3.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.